package com.renbang.utils.mail;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;

/**
 * 邮件发送工具实现类
 * 
 * @author shadow
 * @create 2013/07/12
 */
public class MailUtil {

	protected final Logger logger = Logger.getLogger(getClass());

	public static void main(String[] args) {
		MailBean mailBean = new MailBean();
		mailBean.setSubject("这是一份测试邮件");
		mailBean.setMessage("<HTML><HEAD><TITLE>标签<TITLE></HEAD><BODY><h1>欢迎欢迎欢迎你</h1></BODY></HTML>");
		mailBean.setReceiver("1575772007@qq.com");
		new MailUtil().sendHtmlEmail(mailBean);
	}

	public boolean sendHtmlEmail(MailBean mail)  {
		HtmlEmail htmlEmail = new HtmlEmail();
		htmlEmail.setHostName(mail.getHost()); // Yahoo的服务器地址是：smtp.mail.yahoo.com
		try {
			htmlEmail.addTo(mail.getReceiver());
			htmlEmail.setFrom(mail.getSender());
			htmlEmail.setAuthentication(mail.getSender(), mail.getPassword());
			htmlEmail.setCharset(MailBean.ENCODEING);
			htmlEmail.setSubject(mail.getSubject());
			String msg = mail.getMessage();
			htmlEmail.setHtmlMsg(msg);
			htmlEmail.send();
		} catch (EmailException e) {
			e.printStackTrace();
			logger.info(mail.getSender() + " 发送邮件到 " + mail.getReceiver()
					+ " 失败");
			return false;
		}
		return false;

	}

	public boolean send(MailBean mail) {
		// 发送email
		HtmlEmail email = new HtmlEmail();
		try {
			// 这里是SMTP发送服务器的名字：163的如下："smtp.163.com"
			email.setHostName(mail.getHost());
			// 字符编码集的设置
			email.setCharset(MailBean.ENCODEING);
			// 收件人的邮箱
			email.addTo(mail.getReceiver());
			// 发送人的邮箱
			email.setFrom(mail.getSender());
			// 如果需要认证信息的话，设置认证：用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
			email.setAuthentication(mail.getUsername(), mail.getPassword());
			// 要发送的邮件主题
			email.setSubject(mail.getSubject());
			// 要发送的信息，由于使用了HtmlEmail，可以在邮件内容中使用HTML标签
			email.setMsg(mail.getMessage());
			// 发送
			email.send();
			if (logger.isDebugEnabled()) {
				logger.debug(mail.getSender() + " 发送邮件到 " + mail.getReceiver());
			}
			return true;
		} catch (EmailException e) {
			e.printStackTrace();
			logger.info(mail.getSender() + " 发送邮件到 " + mail.getReceiver()
					+ " 失败");
			return false;
		}
	}

}